package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")RegionQuery qo){
        // page
        Page<Region> list = regionService.query(qo);

        model.addAttribute("page",list);
        return "region/list";
    }

    @RequestMapping("/listDest")
    @ResponseBody
    public Object listDest(){
        // 查找所有的目的地
        return destinationService.listAll();
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        if (StringUtils.hasLength(region.getId())) {
            // 编辑
            // 全量更新（若前端传过来的region 属性有空 或者 提交表单的时候没有其中一些属性内容，直接更新会覆盖原本的东西）
            regionService.update(region);
        }else {
            // 保存
            // region.setId(null);
            regionService.save(region);
        }
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        regionService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot,String id){
        regionService.changeHotValue(hot,id);
        return JsonResult.success();
    }

    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){   // 关联地区
        return destinationService.getDestByRegionId(rid);
    }
}
